package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.widget.CheckBox
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.GoodsDetailCouponListResponse
import com.gexiaobao.pigeon.databinding.ItemGoodsDetailCouponBinding

/**
 * @Author      : hxw
 * @Date        : 2023/6/28 14:58
 * @Describe    :
 */
class DetailCouponListAdapter(data: ArrayList<GoodsDetailCouponListResponse.ListBean>, goodName: String) :
    BaseQuickAdapter<GoodsDetailCouponListResponse.ListBean, DataBindBaseViewHolder>(R.layout.item_goods_detail_coupon, data) {

    private var mCheckedListener: OnCheckedClickListener? = null
    private val checkList = ArrayList<GoodsDetailCouponListResponse.ListBean>()
    private val newList = ArrayList<GoodsDetailCouponListResponse.ListBean>()
    private var mGoodName = ""

    init {
        this.mGoodName = goodName
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: GoodsDetailCouponListResponse.ListBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemGoodsDetailCouponBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        val checkBoxCoupon = holder.getView<CheckBox>(R.id.checkBoxCoupon)
        val couponUseType = if (item.useTypeLabel.contains("店铺")) {
            context.getString(R.string.Limited_to_selected_stores_only)//仅限指定点【可用】
        } else if (item.useTypeLabel.contains("类型")) {
            context.getString(R.string.Type_only)//"仅限指定类型【可用】"
        } else {
            context.getString(R.string.only) + mGoodName + context.getString(R.string.usable)//"仅限$mGoodName【可用】"
        }
        holder.setText(R.id.tvGoodsName, couponUseType)
        val coupon = data[getItemPosition(item)]
        checkBoxCoupon.isChecked = coupon.isChecked

        checkBoxCoupon.setOnClickListener {
            if (data[getItemPosition(item)].isChecked) {
                coupon.isChecked = false
                notifyItemChanged(getItemPosition(item))
            } else {
                coupon.isChecked = true
                notifyItemChanged(getItemPosition(item))
            }
            checkList.clear()
            newList.clear()
            data.forEach {
                if (it.isChecked) {
                    checkList.add(it)
                } else {
                    checkList.remove(it)
                }
                newList.add(it)
            }
            mCheckedListener?.onBoxCheckedListener(holder.layoutPosition, checkList, newList)
        }
        checkBoxCoupon.isEnabled = item.isAllow
    }

    interface OnCheckedClickListener {
        fun onBoxCheckedListener(
            position: Int,
            list: List<GoodsDetailCouponListResponse.ListBean>,
            newList: List<GoodsDetailCouponListResponse.ListBean>
        )
    }

    fun setOnBoxCheckedClickListener(onCheckedClick: OnCheckedClickListener) {
        mCheckedListener = onCheckedClick
    }
}